using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ProtectiveItemListDisplay : MonoBehaviour {
	
	public GameObject itemDisplayTemplate;
	
	List<GameObject> itemDisplays;
	
	public UITable table;
	
	public bool toggled = false;
	
	WeaponItem selectedWeapon;
	
	public Color equippedColor;
	public Color notEquippedColor;

	// Use this for initialization
	void Start () {
		
		itemDisplayTemplate.SetActiveRecursively(false);
		
		itemDisplays = new List<GameObject>();
		
		UIButtonMessage message = gameObject.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "ToggleProtectiveDisplays";
		
		table.repositionNow = true;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void ToggleProtectiveDisplays()
	{
		if(!toggled)
		{
			itemDisplayTemplate.SetActiveRecursively(true);
			
			List<InventoryItem> sortedList = SheetManager.activeCharacter.protectiveItems;

			sortedList.Sort((w1,w2) => w1.name.CompareTo(w2.name));
			
			GameObject go = new GameObject();
			go.transform.parent = transform.parent;
			go.transform.localPosition = Vector3.zero;
			go.transform.localScale = Vector3.one;
			go.name = itemDisplayTemplate.name;
			itemDisplays.Add(go);
			
			foreach(InventoryItem pItem in sortedList)
			{
				go = Instantiate(itemDisplayTemplate) as GameObject;
				go.transform.parent = transform.parent;
				go.transform.localPosition = itemDisplayTemplate.transform.localPosition;
				go.transform.localScale = itemDisplayTemplate.transform.localScale;
				go.name += pItem.name;
				
				ProtectiveItemListDisplayTemplate template = go.GetComponent<ProtectiveItemListDisplayTemplate>();
				template.iname.text = pItem.name;
				template.info.text = pItem.protectiveItemDescription;
				template.slot.text = pItem.slot;
				template.iname.maxHeight = 40;
				template.pItem = pItem;
				
				if(pItem.isEquipped)
				{
					template.buttonLabel.text = "Equipped";
					template.buttonBG.color = equippedColor;
				}
				else
				{
					template.buttonLabel.text = "Not Equipped";
					template.buttonBG.color = notEquippedColor;
				}	
				
				UIButtonMessage message = template.button.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "ToggleItemEquip";
				
				message = go.AddComponent<UIButtonMessage>();
				message.target = gameObject;
				message.functionName = "DisplayItemInfo";
				
				itemDisplays.Add(go);
			}
			
			itemDisplayTemplate.SetActiveRecursively(false); 
			
			table.repositionNow = true;
			toggled = true;
		}
		else
		{
			foreach(GameObject go in itemDisplays)
			{
				Destroy(go);
			}
			itemDisplays.Clear();
			table.repositionNow = true;
			toggled = false;
		}
	}
	
	void ToggleItemEquip(GameObject target)
	{
		ProtectiveItemListDisplayTemplate template = target.transform.parent.GetComponent<ProtectiveItemListDisplayTemplate>();
		template.pItem.EquipToggle();
		
		if(template.pItem.isEquipped)
		{
			template.buttonLabel.text = "Equipped";
			template.buttonBG.color = equippedColor;
		}
		else
		{
			template.buttonLabel.text = "Not Equipped";
			template.buttonBG.color = notEquippedColor;
		}
		
		SheetManager.SaveActiveCharacter();
	}
	
	void DisplayItemInfo(GameObject target)
	{
		GameObject g = Instantiate(GameReference.reference.NewItemWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		BrowserManager.currentNewTransform = g.transform;
		g.transform.localPosition = BrowserManager.CurrentIndex;
		
		ItemEditorMenu w = g.GetComponent<ItemEditorMenu>();
		w.target = gameObject;
		w.functionName = "Do Nothing";
		w.LoadItemDisplay(target.GetComponent<ProtectiveItemListDisplayTemplate>().pItem.name, true);
	}
	
	void ManageInventory()
	{
		
	}
}
